package com.iseastar.guojiang.newcabinet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.NewParcelBean;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

import java.util.ArrayList;
import java.util.List;

import droid.frame.utils.GlideUtil;
import droid.frame.view.ViewPagerExt;

/**
 * Created by joker on 2017/12/23.
 * 查看包裹拍照后的图片
 */
public class ParcelImageLargeActivity extends BaseActivity2 implements View.OnClickListener {

    private ViewPagerExt mViewPager;
    private TextView mCurrentDataTV;//显示当前数据和当前的页数
    private NewParcelBean parcelBean = null;//传过来的对象
    private int currentPosition = 0;//当前操作的对象
    private int imageCurrentPosition = 0;//当前点击图片的位置
    private ArrayList<String> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 第二个方法在此参数无效
        overridePendingTransition(R.anim.in_bottom2top, 0);
        parcelBean = (NewParcelBean) getIntent().getSerializableExtra("item");
        currentPosition = getIntent().getIntExtra("currentPosition", 0);
        imageCurrentPosition = getIntent().getIntExtra("imageCurrentPosition", 0);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_parcel_image_large);
        super.findViewById();
        mViewPager = (ViewPagerExt) findViewById(R.id.view_pager);
        mCurrentDataTV = (TextView) findViewById(R.id.current_data_tv);
        findViewById(R.id.delete_tv).setOnClickListener(this);
        if (parcelBean != null) {
            if (isNotEmpty(parcelBean.getBagImg1())) {
                mList.add(parcelBean.getBagImg1());
            }
            if (isNotEmpty(parcelBean.getBagImg2())) {
                mList.add(parcelBean.getBagImg2());
            }
            if (isNotEmpty(parcelBean.getBagImg3())) {
                mList.add(parcelBean.getBagImg3());
            }
            initViewPager();
            mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    imageCurrentPosition = position;
                    mCurrentDataTV.setText((position + 1) + "/" + mList.size());
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        } else {
            finish();
        }
    }

    private void initViewPager() {
        List<View> pager = new ArrayList<View>();
        for (int i = 0; i < mList.size(); i++) {
            ImageView parms = new ImageView(getContext());
            parms.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            GlideUtil.loadImage(getContext(), mList.get(i), 0, parms);
            pager.add(parms);
        }
        mViewPager.setViewItems(pager, false);
        mViewPager.setCurrentItem(imageCurrentPosition);
        mCurrentDataTV.setText((imageCurrentPosition + 1) + "/" + mList.size());
    }

    @Override
    public void finish() {
        Intent intent = new Intent();
        intent.putExtra("item", parcelBean);
        intent.putExtra("currentPosition", currentPosition);
        setResult(Activity.RESULT_OK, intent);
        super.finish();
        overridePendingTransition(0, R.anim.out_top2bottom);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delete_tv:
                if (parcelBean != null) {
                    parcelBean.setDeleteParcelImage(mList.get(imageCurrentPosition));
                    showLoadingDialog(null);
                    AppHttp.getInstance().courierParcelImageData(parcelBean.getDetailId(), 1, mList.get(imageCurrentPosition), currentPosition);
                }
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.courier_parcel_process_image_data + 1:
                cancelLoadingDialog();
                final ReqResult<?> deleteImageResult = JSON.parser(msg.obj);
                if (checkLoginStatus(deleteImageResult)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if ((Boolean) deleteImageResult.getResult()) {
                                if (isNotEmpty(parcelBean.getBagImg1()) && isNotEmpty(parcelBean.getDeleteParcelImage()) && parcelBean.getBagImg1().equals(parcelBean.getDeleteParcelImage())) {
                                    parcelBean.setBagImg1("");
                                } else if (isNotEmpty(parcelBean.getBagImg2()) && isNotEmpty(parcelBean.getDeleteParcelImage()) && parcelBean.getBagImg2().equals(parcelBean.getDeleteParcelImage())) {
                                    parcelBean.setBagImg2("");
                                } else if (isNotEmpty(parcelBean.getBagImg3()) && isNotEmpty(parcelBean.getDeleteParcelImage()) && parcelBean.getBagImg3().equals(parcelBean.getDeleteParcelImage())) {
                                    parcelBean.setBagImg3("");
                                }
                                mList.remove(parcelBean.getDeleteParcelImage());
                                parcelBean.setDeleteParcelImage("");
                                imageCurrentPosition = 0;
                                if (mList.size() == 0) {
                                    finish();
                                } else {
                                    initViewPager();
                                }
                            } else {
                                showToast(deleteImageResult.getMessage());
                            }
                        }
                    });
                } else {
                    showToast(deleteImageResult.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }

    }
}
